package com.example.texted.countdownlatch;

import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
//让异步执行的逻辑一起汇报
public class Runner implements Callable<Integer> {
    private CountDownLatch begin;

    private CountDownLatch end;

    public Runner(CountDownLatch begin, CountDownLatch end) {
        this.begin = begin;
        this.end = end;
    }

    @Override
    public Integer call() throws Exception {
        //跑步的成绩
        int nextInt = new Random().nextInt(100);
        //等待发令枪响起
        begin.await();
        //跑步中……
        TimeUnit.MILLISECONDS.sleep(nextInt);
        //跑步者已跑完全程
        end.countDown();
        return nextInt;
    }
}
